package com.qining618.cms.cms.controller.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qining618.cms.cms.domain.Content;
import com.qining618.cms.cms.service.ContentCountService;
import com.qining618.cms.cms.service.ContentService;
import com.qining618.common.annotation.Access;
import com.qining618.common.context.ContextHandle;
import com.qining618.common.result.ApiResult;
import com.qining618.common.result.TableData;
import com.qining618.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * CMS内容 前端控制器
 * </p>
 *
 * @author qining
 * @date 2019-09-17
 */
@RestController
@RequestMapping("/app/api/cms/content")
public class AppContentController {

    @Autowired
    private ContentService contentService;
    @Autowired
    private ContentCountService contentCountService;

    @GetMapping(value = "page")
    @Description("分页")
    public ApiResult page(@RequestParam Map<String, Object> params) {
        Query<Content> query = new Query<>(params);

        IPage<Content> pageInfo = contentService.page(query.getPage(), query.getQueryWrapper());

        return ApiResult.ok(TableData.of(pageInfo));
    }

    @Access
    @PutMapping("{id}/up")
    @Description("顶")
    public ApiResult up(@PathVariable(value = "id") Long id) {
        Long userId = ContextHandle.getUserId();
        int ups = contentCountService.contentUp(id, userId);
        return ApiResult.ok(ups);
    }

    @Access
    @PutMapping("{id}/down")
    @Description("踩")
    public ApiResult down(@PathVariable(value = "id") Long id) {
        Long userId = ContextHandle.getUserId();
        int ups = contentCountService.contentDown(id, userId);
        return ApiResult.ok(ups);
    }
}

